﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Greg.WPF.Charts
{
    /// <summary>
    /// Interaction logic for dragControl.xaml
    /// </summary>
    public partial class DragControl : UserControl
    {
        private bool rightMouseButtonIsDown;
        private bool leftMouseButtonIsDown;
        private Point delta = new Point(0,0);
        private Point initialPosition = new Point(0,0);
        private Point lastPosition = new Point(0,0);

        public delegate void MouseDragEventHandler(object sender, DeltaMouseEventArgs e);

        public class DeltaMouseEventArgs : EventArgs
        {
            public Point InitialPosition;
            public Point Delta;
        }
        public event MouseDragEventHandler MouseDragLeft;
        public event MouseDragEventHandler MouseDragRight;
        public event MouseButtonEventHandler MouseDoubleClick;


        public DragControl()
        {
            InitializeComponent();
        }

        private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if(MouseDoubleClick != null) MouseDoubleClick(this, e);            
        }

        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2) if (MouseDoubleClick != null) MouseDoubleClick(this, e); 
 
            if (((UIElement)sender).CaptureMouse() && !(rightMouseButtonIsDown) && e.ClickCount == 1) 
            {
                leftMouseButtonIsDown = true;
                lastPosition.X = initialPosition.X = e.GetPosition(this).X;
                lastPosition.Y = initialPosition.Y = e.GetPosition(this).Y;
                MouseMove += UserControl_MouseMove;
            }      
        }

        private void UserControl_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {


            if (((UIElement)sender).CaptureMouse() && !(leftMouseButtonIsDown))
            {
                rightMouseButtonIsDown = true;
                lastPosition.X = initialPosition.X = e.GetPosition(this).X;
                lastPosition.Y = initialPosition.Y = e.GetPosition(this).Y;
                MouseMove += UserControl_MouseMove;
            }
        }

        private void UserControl_MouseMove(object sender, MouseEventArgs e)
        {
            delta.X = e.GetPosition(this).X - lastPosition.X;         
            delta.Y = e.GetPosition(this).Y - lastPosition.Y;
            lastPosition.X = e.GetPosition(this).X;
            lastPosition.Y = e.GetPosition(this).Y;
            DeltaMouseEventArgs dme = new DeltaMouseEventArgs();
            dme.Delta = delta;
            dme.InitialPosition = initialPosition;
            if (leftMouseButtonIsDown && MouseDragLeft != null) MouseDragLeft(this, dme);
            if (rightMouseButtonIsDown && MouseDragRight != null) MouseDragRight(this, dme);
        }

        private void UserControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MouseMove -= UserControl_MouseMove;
            ((UIElement)sender).ReleaseMouseCapture();
            leftMouseButtonIsDown = false;
            
        }

        private void UserControl_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            MouseMove -= UserControl_MouseMove;
            rightMouseButtonIsDown = false;
            ((UIElement)sender).ReleaseMouseCapture();
        }
    }
}
